<?php
define('ROOT_PATH', str_replace('services/corn.php', '', str_replace('\\', '/', __FILE__)));
$ajax_result_ok = '{"status":1}';
$ajax_result_err = '{"status":0}';

if(!file_exists(ROOT_PATH.'./public/cron.lock') || file_exists(ROOT_PATH.'./public/run.lock'))
{
	if(file_exists(ROOT_PATH.'./public/cron.lock') && file_exists(ROOT_PATH.'./public/run.lock'))
		echo $ajax_result_ok;
	else
		echo $ajax_result_err;
	exit;
}

define('SUB_DIR','/services');
require '../core/service/fanwe.service.php';
$fanwe = &FanweService::instance();
$fanwe->is_group_city = false;
$fanwe->is_session = false;
$fanwe->is_user = false;
$fanwe->is_cron = false;
$fanwe->cache_list = array();
$fanwe->initialize();

global $_FANWE;

$ajax_result_ok = '{"status":1}';
$ajax_result_err = '{"status":0}';

set_time_limit(3600);
if(function_exists('ini_set'))
	ini_set('max_execution_time',3600);

$site_id = DB::resultFirst('SELECT id FROM '.DB::table('cron_site').' LIMIT 0,1');
$site_id = intval($site_id);
if($site_id == 0)
{
	@unlink(FANWE_ROOT.'./public/cron.lock');
	@unlink(FANWE_ROOT.'./public/run.lock');
	echo $ajax_result_err;
	exit;
}

DB::query('DELETE FROM '.DB::table('cron_site').' WHERE id = '.$site_id);
if(DB::affectedRows() == 0)
{
	echo $ajax_result_ok;
	exit;	
}

file_put_contents(FANWE_ROOT.'./public/run.lock','run');
if(!file_exists(FANWE_ROOT.'./public/run.lock'))
{
	DB::query('REPLACE INTO '.DB::table('cron_site').'(id) VALUES('.$site_id.')');
	echo $ajax_result_ok;
	exit;
}
else
{
	@chmod(FANWE_ROOT.'./public/run.lock', 0777);
}

try
{
	loadCache('goods');
	DB::query('TRUNCATE TABLE '.DB::table('goods_temp'));
	DB::query('REPLACE INTO '.DB::table('goods_temp').'
			SELECT id, site_id, city, cate_id, sort, @sort:=@sort + 1 as r_sort,0,0,0,0,0,end_time 
			FROM '.DB::table('goods_now_key').' FORCE INDEX (site),(SELECT @sort:=0) AS t 
			WHERE site_id = '.$site_id.' ORDER BY sort ASC,id DESC');
	
	if(DB::affectedRows() > 0)
	{
		$goods_cate_citys = array();
		$goods_cates = array();
		$goods_citys = array();
		$sql = '';
		$sql_jg = '';
		$res = DB::query('SELECT * FROM '.DB::table('goods_temp').' ORDER BY sort ASC,id DESC');
		while($goods = DB::fetch($res))
		{
			$cr_sort = 0;
			$d_sort = 0;
			$dc_sort = 0;
			$e_sort = 0;
			$ec_sort = 0;
			
			$city = $goods['city'];
			if(!isset($goods_citys[$city]))
				$goods_citys[$city] = 0;
			$goods_citys[$city]++;
			$cr_sort = $goods_citys[$city];
			
			$cate_id = $goods['cate_id'];
			$cate = $_FANWE['cache']['goods']['cats'][$cate_id];
			if(!isset($goods_cates[$cate_id]))
				$goods_cates[$cate_id] = 0;
			$goods_cates[$cate_id]++;
			
			if(!isset($goods_cate_citys[$cate_id][$city]))
				$goods_cate_citys[$cate_id][$city] = 0;
			$goods_cate_citys[$cate_id][$city]++;	
			
			$parent_id = $cate['pid'];
			if($parent_id > 0)
			{
				$e_sort = $goods_cates[$cate_id];
				$ec_sort = $goods_cate_citys[$cate_id][$city];
				
				if(!isset($goods_cates[$parent_id]))
					$goods_cates[$parent_id] = 0;
				$goods_cates[$parent_id]++;
				
				$d_sort = $goods_cates[$parent_id];
				
				if(!isset($goods_cate_citys[$parent_id][$city]))
					$goods_cate_citys[$parent_id][$city] = 0;
				$goods_cate_citys[$parent_id][$city]++;
				
				$dc_sort = $goods_cate_citys[$parent_id][$city];
			}
			else
			{
				$d_sort = $goods_cates[$cate_id];
				$dc_sort = $goods_cate_citys[$cate_id][$city];
			}
			
			$sql .= "$sql_jg($goods[id],$goods[site_id], '$city', $cate_id, $goods[sort], $goods[r_sort], $cr_sort, $d_sort, $dc_sort, $e_sort, $ec_sort, $goods[end_time])";
			$sql_jg = ',';
		}
		
		if($sql != '')
		{
			DB::query('REPLACE INTO '.DB::table('goods_temp').' VALUES '.$sql);
			DB::query('REPLACE INTO '.DB::table('goods_now_key').' SELECT * FROM '.DB::table('goods_temp'));
		}
	}
	
	usleep(10);
	DB::query('TRUNCATE TABLE '.DB::table('goods_temp'));
	DB::query('REPLACE INTO '.DB::table('goods_temp').'
			SELECT id, site_id, city, cate_id, sort, @sort:=@sort + 1 as r_sort,0,0,0,0,0,end_time 
			FROM '.DB::table('goods_key').' FORCE INDEX (site),(SELECT @sort:=0) AS t 
			WHERE site_id = '.$site_id.' AND end_time > '.getTodayTime().' 
			ORDER BY sort ASC,id DESC');
	if(DB::affectedRows() > 0)
	{
		$goods_cate_citys = array();
		$goods_cates = array();
		$goods_citys = array();
		$sql = '';
		$sql_jg = '';
		$res = DB::query('SELECT * FROM '.DB::table('goods_temp').' ORDER BY sort ASC,id DESC');
		while($goods = DB::fetch($res))
		{
			$cr_sort = 0;
			$d_sort = 0;
			$dc_sort = 0;
			$e_sort = 0;
			$ec_sort = 0;
			
			$city = $goods['city'];
			if(!isset($goods_citys[$city]))
				$goods_citys[$city] = 0;
			$goods_citys[$city]++;
			$cr_sort = $goods_citys[$city];
			
			$cate_id = $goods['cate_id'];
			$cate = $_FANWE['cache']['goods']['cats'][$cate_id];
			if(!isset($goods_cates[$cate_id]))
				$goods_cates[$cate_id] = 0;
			$goods_cates[$cate_id]++;
			
			if(!isset($goods_cate_citys[$cate_id][$city]))
				$goods_cate_citys[$cate_id][$city] = 0;
			$goods_cate_citys[$cate_id][$city]++;	
			
			$parent_id = $cate['pid'];
			if($parent_id > 0)
			{
				$e_sort = $goods_cates[$cate_id];
				$ec_sort = $goods_cate_citys[$cate_id][$city];
				
				if(!isset($goods_cates[$parent_id]))
					$goods_cates[$parent_id] = 0;
				$goods_cates[$parent_id]++;
				
				$d_sort = $goods_cates[$parent_id];
				
				if(!isset($goods_cate_citys[$parent_id][$city]))
					$goods_cate_citys[$parent_id][$city] = 0;
				$goods_cate_citys[$parent_id][$city]++;
				
				$dc_sort = $goods_cate_citys[$parent_id][$city];
			}
			else
			{
				$d_sort = $goods_cates[$cate_id];
				$dc_sort = $goods_cate_citys[$cate_id][$city];
			}
			
			$sql .= "$sql_jg($goods[id],$goods[site_id], '$city', $cate_id, $goods[sort], $goods[r_sort], $cr_sort, $d_sort, $dc_sort, $e_sort, $ec_sort, $goods[end_time])";
			$sql_jg = ',';
		}
		
		if($sql != '')
		{
			DB::query('REPLACE INTO '.DB::table('goods_temp').' VALUES '.$sql);
			DB::query('REPLACE INTO '.DB::table('goods_key').' SELECT * FROM '.DB::table('goods_temp'));
		}
	}
	
	DB::query('TRUNCATE TABLE '.DB::table('goods_temp'));
	
	$count = intval(DB::resultFirst('SELECT COUNT(id) FROM '.DB::table('cron_site')));
	if($count == 0)
	{
		@unlink(FANWE_ROOT.'./public/cron.lock');
		S('Goods')->updateAuctionRank();
	}
}
catch(Exception $e){}

@unlink(FANWE_ROOT.'./public/run.lock');
echo $ajax_result_ok;
?>